1. Basic overviewSet Map.entry K , V >> entryset() returns a set view of the mappings included in this map. Set K > keySet() returns a set view of the keys included in this map. 2. Efficiency analysisFor keyset in fact it is traversed 2 times, one is converted to iterator. Remove the value of key from the HashMap one time.and EntrySet just traversed for the first time, he put the key and value in the ent
Keyset is a collection of keys, the type of the set is the type of keyEntrySet is a set of key-value pairs, and the type inside the set is Map.entry1.keySet ()Map map=new HashMap ();Iterator It=map.keyset (). Iterator ();Object key;Object value;while (It.hasnext ()) {Key=it.next ();Value=map.get (key);System.out.println (key+ ":" +value);}2.entrySet ()Map map=new HashMap ();Iterator It=map.entryset (). Iterator ();Object key;Object value;while (It.has
ImportJava. util. calendar;
ImportJava. util. date;
ImportJava. util. hashmap;
ImportJava. util. iterator;
ImportJava. util. Map. entry;
/**
* Test the iteration time of keyset () and entryset ().
* Keyset (): The key can only be obtained through get () after iteration.
* Entryset (): After iteration, you can use E. getkey () and E. getvalue () to get the key and value. The entry interface is retu
1, keyset () speed than EntrySet () a lot slower, that is, keyset way to traverse the map performance is not as good as entryset performance in order to improve performance, and later consider using the EntrySet () way to traverse. For keyset, he facilitated two times, one to iterator type, once into the hashmap to take out the value of the key, two
()) {Entry//get a Entry objectEntry.setvalue ("123");//replaces the value of the entry of the current iteration with the 123String Key=entry.getkey ();//gets the key for the current iteration of the Entry object (entry in the returned set view)String Value=entry.getvalue ();//gets the value of the current iteration entry objectSystem.out.println (key+ "=" +value); Entry.setvalue ("1");//this replaces the value of the current iteration object, which is mapped to the hashmap} System.out.println (
hasmoreelemerts (): Tests whether the enumeration enumeration object also contains elements, and if true, indicates that there are at least one element.· Object Nextelement (): If the Bnumeration enumeration object also contains elements, the method gets the next element in the object.Iterator interface Main method:
boolean
hasNext()Returns true If the iteration have more elements.
E
next()Returns the next element in the iteration.
void
Java Tour (22)--map Overview, sub-class object characteristics, common methods, keyset,entryset,map small exercises
Keep on going, ladies and gentlemen!
In fact, our data structure, only the knowledge point of this map, usually developed, but also can see him, so it is very worthwhile to learn a point of knowledge, we drove directly
I. Map overview
Generic
Basic Features
The set stores key-value pairs, is
/** Two ways to remove a map collection: (Map without iterator method)* 1, set* So you can iterate through all the keys, and then according to the Get method, get each key corresponding value.* The extraction principle of the Map collection: The Map collection is transferred to the set set and then removed by the iterator* 2, Set* and the data type of the relationship is: map.entry*/Package Map overview; import java.util.hashmap;import java.util.iterator;import java.util.map;import java.util.set
In the Map collection
VALUES (): Method is to get all the values in the collection----no key, no correspondence, keyset (): All the keys in the map are saved to the set collection. Because the set has an iterator. All the keys can be iterated out and then based on the Get method. Gets the corresponding value for each key. Keyset (): The key can only be fetched via get () after iterationEntrySet (): set
Here's an example to see:
public class Map {public static void main (string[] args) {mapDescr
HashMap EntrySet and keyset Category: JAVA base 2011-03-01 12:04 96 people read Comments (0) Collection report
When you need to traverse HashMap, please try to use EntrySet, instead of using keyset,entryset more efficient than keyset, actually use EntrySet is only need to traverse a hash, The mapping of key and value
EntrySet () method returns a special set, defined as the internal private class of the HashMap
Private Final class EntrySet extends abstractsetTake a look at this set's iterator () method mainly. This method is simple, and returns an instance of the Entryiterator type. The Entryiterator type is a subclass of the generic hashiteratorHashMap | table
EntrySet () |i
Today on the Internet to see a traversal hashmap to value, with keyset efficiency than with entryset low, and then tested again, found that in fact can not generalize
The following code is executed multiple times, with different values
According to the online example, Hm.put (i, "Thans");
EntrySet: Elapsed Time MS: 16Keyset: Elapsed time MS: 31
EntrySet: Elapsed
One.There is another way to do this in the previous section, which is a bit of a hassle.From the action just now, summed up a principle. The Map collection has no iterators, but we can convert the map to a collection of iterators, such as set.What is this for? Why not return arrylist, which is to take all the keys, the key is unique, so the return should be set set, to ensure that the uniqueness of the element.Next, in the API, there is also a way to return the set set, ( what is the special mea
Keyset is the set of keys, the type of the set is the type of keyEntrySet is a set of key-value pairs, and the type within set is Map.entry
1.keySet ()
Map map=new HashMap ();
Iterator It=map.keyset (). iterator ();
Object key;
Object value;
while (It.hasnext ()) {
key=it.next ();
Value=map.get (key);
System.out.println (key+ ":" +value);
}
2.entrySet ()
Map map=new HashMap ();
Iterator It=map.entryset (). iterator ();
Object key;
Object value;
wh
There are various map1 in Java, and one of the most efficient traversal methods is as follows:treemapOrtreemapThis use of the EntrySet traversal method is less complex than using keyset with use.This article is from the "Science-mymind" blog, make sure to keep this source http://qkkcoolmax.blog.51cto.com/8843422/1615010The entryset of a good way to traverse map in Java
Based on the new features of JDK5, use a for loop map, such as the Key for loop map
123
for(String dataKey : paraMap.keySet()) {System.out.println(dataKey );}
Note how paramap is defined, if it is a simple map paramap = new HashMap (); The string in front can only be converted to object.The key and value of the entire map
123456789101112
Mapnew LinkedHashMapmap.put(1,"星期一");map.put(2,"星期二");map.put(3,"星期三");map.put(4,"星期四");map.put(5,"星期五");map.pu
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.